package com.gurun.camera.account.presenter;

import android.text.TextUtils;

import com.danale.sdk.platform.constant.user.UserType;
import com.danale.sdk.platform.result.countrycode.CountryCodeResult;
import com.danale.sdk.platform.result.user.ForgetPasswordResult;
import com.danale.sdk.platform.result.user.RegistGetVerificationCodeResult;
import com.danale.sdk.throwable.PlatformApiError;
import com.gurun.camera.account.model.ForgotModelImpl;
import com.gurun.camera.account.model.IForgotModel;
import com.gurun.camera.account.model.ISendVerifyCodeModel;
import com.gurun.camera.account.model.ObtainCountryModelImpl;
import com.gurun.camera.account.model.SendVerifycodeModelImpl;
import com.gurun.camera.account.utils.PatternMatchUtil;
import com.gurun.camera.account.view.IAccountView;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by danal on 2016/9/9.
 */

public class VerifyAccountPresenterImpl implements IVerifyAccountPresenter {
    public static int REGIST_SEND_CODE = 1;
    public static int FORGOT_SEND_CODE = 2;
    private IAccountView iAccountView;
    private ISendVerifyCodeModel sendVerifyCodeModel;
    private IForgotModel iForgotModel;
    private ObtainCountryModelImpl obtainCountryModel;


    public enum IllegalType {
        /**
         * 账号空
         */
        ACC_NULL,

        /**
         * 账号存在
         */
        ACC_EXIST,

        /**
         * 账号不合法
         */
        ACC_ILLEGAL;


    }

    public VerifyAccountPresenterImpl(IAccountView v) {
        this.iAccountView = v;
        sendVerifyCodeModel = new SendVerifycodeModelImpl();
        iForgotModel = new ForgotModelImpl();
        obtainCountryModel = new ObtainCountryModelImpl();

    }

    /**
     * 检查账户类型 邮箱 or 手机
     * Check the account type ,account is phone number user can choose country code
     * @param account 账户
     *                phone number or email
     */

    @Override
    public void verifyAccountType(String account) {
        iAccountView.hideIllegalLayout();
        if (TextUtils.isEmpty(account)) {
            iAccountView.hideCountry();
            return;
        }

        if (PatternMatchUtil.isNumber(account)) {
            iAccountView.showCountryCode();
            iAccountView.showCountryFlag();

        } else {
            iAccountView.hideCountry();
        }

    }

    /**
     * 检查账户是否合法
     * Check whether the account is legal
     * @param account
     * @param sendtype  1，注册获取验证码  2，忘记密码获取验证码
     *                  1.rigister to get verification code 2.forgot to get verification code
     */

    @Override
    public void verifyAccountLegal(String account, int sendtype, String countrycode) {

        if (TextUtils.isEmpty(account)) {
            iAccountView.notifyIllegalAccount(IllegalType.ACC_NULL);
            return;
        }
        if (PatternMatchUtil.isEmailAddress(account)) {

            /**
             * 邮箱注册
             */
            sendVerifycode(account, UserType.EMAIL, "", sendtype);


        } else if (PatternMatchUtil.isNumber(account)) {

            /**
             * 手机注册
             */
            sendVerifycode(account, UserType.PHONE, countrycode, sendtype);


        } else {

            iAccountView.notifyIllegalAccount(IllegalType.ACC_ILLEGAL);

        }


    }

    /**
     * 获取验证码
     * get verification code
     * @param account
     * @param type 账户类型 邮箱 或 手机
     *             Account type email or phone
     * @param countrycode 账户为邮箱时：null
     *                    If userName is the phone number, phonecode is the country code of the country where the phone number belongs.
     *                    If userName is a email, phonecode is empty;
     * @param sendtype  1，注册获取验证码  2，忘记密码获取验证码
     *                  1.rigister to get verification code 2.forgot to get verification code
     */

    @Override
    public void sendVerifycode(String account, UserType type, String countrycode, int sendtype) {
        if (iAccountView != null) {
            iAccountView.showSendVerifycodeProgressBar();
        }


        if (sendtype == REGIST_SEND_CODE) {

            sendVerifyCodeModel.sendVerifyCode(account, type, countrycode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<RegistGetVerificationCodeResult>() {
                        @Override
                        public void call(RegistGetVerificationCodeResult registGetVerificationCodeResult) {

                            if (iAccountView != null) {
                                iAccountView.sendVerifycodeResult("SUCCESS");
                                iAccountView.hideSendVerifycodeProgressBar();
                            }
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {

                            if (throwable instanceof PlatformApiError) {
                                if (iAccountView != null) {
                                    iAccountView.sendVerifycodeResult(((PlatformApiError) throwable).getErrorDescription());
                                    iAccountView.hideSendVerifycodeProgressBar();

                                }

                            }
                        }
                    });

        } else if (sendtype == FORGOT_SEND_CODE) {

            iForgotModel.sendForgotPwdCode(account, type, countrycode)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<ForgetPasswordResult>() {
                        @Override
                        public void call(ForgetPasswordResult forgetPasswordResult) {

                            if (iAccountView != null) {
                                iAccountView.sendVerifycodeResult("SUCCESS");
                                iAccountView.hideSendVerifycodeProgressBar();

                            }

                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {

                            if (iAccountView != null) {
                                iAccountView.sendVerifycodeResult(((PlatformApiError) throwable).getErrorDescription());
                                iAccountView.hideSendVerifycodeProgressBar();

                            }

                        }
                    });


        }


    }

    /**
     * 获取当前国别码
     * get current country code
     */
    @Override
    public void getCurrentCountryCode() {
        obtainCountryModel.obtainCountryCodelist().observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CountryCodeResult>() {
                    @Override
                    public void call(CountryCodeResult countryCodeResult) {

                        for (int i = 0; i < countryCodeResult.getCountryCodes().size(); i++) {
                            if (countryCodeResult.getCountryCodes().get(i).isDefault() && iAccountView != null) {
                                iAccountView.noyifyCurrentCountryCode(countryCodeResult.getCountryCodes().get(i));
                            }
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

}
